package com.hulk.dryad.web.oauth2.handler;


import com.hulk.dryad.common.util.WebUtils;
import com.hulk.dryad.manage.framework.tenant.KeyStrResolver;
import com.hulk.dryad.manage.security.handler.AuthenticationSuccessEventHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author hulk
 * @date 2019/10/8
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DryadAuthenticationSuccessEventHandler implements AuthenticationSuccessEventHandler {

	private final KeyStrResolver tenantKeyStrResolver;
	/**
	 * 处理登录成功方法
	 * <p>
	 * 获取到登录的authentication 对象
	 * @param authentication 登录对象
	 * @param request 请求
	 * @param response 返回
	 */

	@Override
	public void handle(Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
		String username = authentication.getName();
		String header = request.getHeader(HttpHeaders.AUTHORIZATION);
		String serviceId = WebUtils.extractClientId(header).orElse("N/A");
		String tenantId = tenantKeyStrResolver.key();

		log.info("用户：[{}] header：[{}] 登录成功, 服务ID：[{}] 租户ID：[{}]。", username,header,serviceId,tenantId);
	}

}
